package com.khyr.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.dao.MenuDao;
import com.khyr.entity.Menu;
import com.khyr.entity.RoleMenu;
import com.khyr.service.MenuService;
import com.khyr.vo.MenuVo;
import com.khyr.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 宋一恒
 * @version 1.0
 */

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;


    @Override
    public R<List<Menu>> selectAllMenu() {
        List<Menu> menuList = menuDao.selectList(null);
        if (menuList != null && menuList.size() > 0){
            return new R<List<Menu>>(200,"查询成功",menuList);
        }
        return new R<>(500,"查询失败",null);
    }

    @Override
    public R<Page<Menu>> findMenuByCond(MenuVo menuVo) {
        //分页
        Page<Menu> page = new Page<>(menuVo.getPageNum(), menuVo.getPageSize());
        //模糊
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(menuVo.getMenuName())){
            queryWrapper.like("menu_name", menuVo.getMenuName());
        }
        if (StringUtils.hasText(menuVo.getStatus())){
            queryWrapper.eq("status", menuVo.getStatus());
        }
        //存放到pageIfo中
        Page<Menu> p = menuDao.selectPage(page, queryWrapper);
        return new R<>(200,"查询成功",p);
    }

    @Override
    public R<Integer> addMenu(Menu menu) {
        int i = menuDao.insert(menu);
        if (i > 0){
            return new R<>(200,"添加成功",i);
        }
        return new R<>(500,"添加失败",null);
    }

    @Override
    public R<Integer> updateMenu(Menu menu) {
        int i = menuDao.updateById(menu);
        if (i > 0){
            return new R<>(200,"修改成功",i);
        }
        return new R<>(500,"修改失败",null);
    }

    @Override
    public R<Menu> getMenuById(Long id) {
        Menu menu = menuDao.selectById(id);
        if (menu != null){
            return new R<>(200,"查询成功",menu);
        }
        return new R<>(500,"查询失败",null);
    }

    @Override
    public R<Integer> deleteMenuById(Long id) {
        int i = menuDao.deleteById(id);
        if (i > 0){
            return new R<>(200,"删除成功",i);
        }
        return new R<>(500,"删除失败",null);
    }

    @Override
    public R<List<RoleMenu>> selectMenusByRoleId(Long roleId) {
        if (roleId != null){
            List<RoleMenu> menuList = menuDao.selectMenusByRoleId(roleId);
            if (menuList != null && menuList.size() > 0){
                return new R<>(200,"查询成功",menuList);
            }
        }
        return null;
    }
}
